package com.qhhc.controller;

import com.qhhc.entity.CarComment;
import com.qhhc.service.CarCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/comments")
@CrossOrigin(origins = "http://localhost:5173")
public class CarCommentController {

    @Autowired
    private CarCommentService carCommentService;

    /**
     * 获取所有评论
     */
    @GetMapping
    public List<CarComment> getAllComments() {
        return carCommentService.getAllComments();
    }

    /**
     * 根据车辆ID获取评论
     */
    @GetMapping("/car/{carId}")
    public List<CarComment> getCommentsByCarId(@PathVariable("carId") Long carId) {
        return carCommentService.getCommentsByCarId(carId);
    }

    /**
     * 根据用户ID获取评论
     */
    @GetMapping("/user/{userId}")
    public List<CarComment> getCommentsByUserId(@PathVariable("userId") Long userId) {
        return carCommentService.getCommentsByUserId(userId);
    }

    /**
     * 根据ID获取评论
     */
    @GetMapping("/{commentId}")
    public CarComment getCommentById(@PathVariable("commentId") Long commentId) {
        return carCommentService.getCommentById(commentId);
    }

    /**
     * 添加评论
     */
    @PostMapping
    public boolean addComment(@RequestBody CarComment comment) {
        return carCommentService.addComment(comment);
    }

    /**
     * 更新评论
     */
    @PutMapping
    public boolean updateComment(@RequestBody CarComment comment) {
        return carCommentService.updateComment(comment);
    }

    /**
     * 删除评论
     */
    @DeleteMapping("/{commentId}")
    public boolean deleteComment(@PathVariable("commentId") Long commentId) {
        return carCommentService.deleteComment(commentId);
    }

    /**
     * 根据车辆ID删除所有评论
     */
    @DeleteMapping("/car/{carId}")
    public boolean deleteCommentsByCarId(@PathVariable("carId") Long carId) {
        return carCommentService.deleteCommentsByCarId(carId);
    }

    /**
     * 根据用户ID删除所有评论
     */
    @DeleteMapping("/user/{userId}")
    public boolean deleteCommentsByUserId(@PathVariable("userId") Long userId) {
        return carCommentService.deleteCommentsByUserId(userId);
    }

    /**
     * 条件查询评论
     */
    @PostMapping("/search")
    public List<CarComment> searchComments(@RequestBody CarComment comment) {
        return carCommentService.getCommentsByCondition(comment);
    }
} 